package com.woniu108.person.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.woniu108.carService.commos.dto.AdminInfoDto;
import com.woniu108.carService.commos.dto.AdminSimpleDto;
import com.woniu108.carService.commos.exception.WoniuException;
import com.woniu108.carService.commos.util.RedisKey;
import com.woniu108.carService.commos.util.RedisUtil;
import com.woniu108.carService.commos.util.StaticData;
import com.woniu108.person.mapper.AdminInfoMapper;
import com.woniu108.person.model.AdminInfo;
import com.woniu108.person.param.AdminLoginParam;
import com.woniu108.person.param.CheckMessageParam;
import com.woniu108.person.param.FilterAdminParam;
import com.woniu108.person.service.AdminInfoService;
import com.woniu108.person.service.AdminRoleService;
import com.woniu108.person.service.StoreService;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 * 管理员信息表 服务实现类
 * </p>
 *
 * @author yanxi
 * @since 2024-05-23
 */
@Service
public class AdminInfoServiceImpl extends ServiceImpl<AdminInfoMapper, AdminInfo> implements AdminInfoService {

    @Resource
    private AdminInfoService adminInfoService;
    @Resource
    private AdminRoleService adminRoleService;
    @Resource
    private StoreService storeService;
    @Resource
    private RedisUtil redisUtil;
    @Resource
    private RabbitTemplate rabbitTemplate;

    @Override
    public Page<AdminInfoDto> allAdmins(FilterAdminParam param) {
        //根据筛选条件筛选数据库中的adminInfo
        QueryWrapper<AdminInfo> wrapper = new QueryWrapper<>();
        if(ObjectUtil.isNotEmpty(param.getAdminState())) wrapper.eq("admin_state",param.getAdminState());
        if(ObjectUtil.isNotEmpty(param.getStoreId())) wrapper.eq("store_id",param.getStoreId());
        if(ObjectUtil.isNotEmpty(param.getAdminName())) wrapper.like("admin_name",param.getAdminName());
        if(ObjectUtil.isNotEmpty(param.getAdminRoleId())) wrapper.eq("admin_role_id",param.getAdminRoleId());
        if(ObjectUtil.isNotEmpty(param.getAdminPhone())) wrapper.like("admin_phone",param.getAdminPhone());
        wrapper.ne("admin_state", StaticData.ADMIN_STATE_DELETE);
        List<AdminInfo> adminInfoList = adminInfoService.list(wrapper);
        List<AdminInfoDto> list = new ArrayList<>();
        //转dto添加到新集合
        for (AdminInfo adminInfo : adminInfoList) {
            AdminInfoDto dto = BeanUtil.toBean(adminInfo, AdminInfoDto.class);
            dto.setAdminRoleName(adminRoleService.getById(dto.getAdminRoleId()).getAdminRoleName());
            if(dto.getStoreId()!=null){
                dto.setStoreName(storeService.getById(dto.getStoreId()).getStoreName());
            }
            list.add(dto);
        }
        //分页
        long total = list.size();
        long pages=total%param.getPageSize()==0?total/param.getPageSize():total/param.getPageSize()+1;
        Page<AdminInfoDto> page=new Page<AdminInfoDto>();
        List<AdminInfoDto> dtos=new ArrayList<>();
        int i=0;
        for (AdminInfoDto infoDto : list) {
            if(i<param.getPageNum()*param.getPageSize().intValue()&&i>=(param.getPageNum()-1)*param.getPageSize().intValue()){
                dtos.add(infoDto);
            }
            i=i+1;
        }
        page.setCurrent(param.getPageNum());
        page.setSize(param.getPageSize());
        page.setRecords(dtos);
        page.setTotal(total);
        page.setPages(pages);
        return page;

    }

    @Override
    public void sendCheckMessage(String adminPhone) {
        //检查一分钟内是否发送过短信
        if(redisUtil.exist(RedisKey.checkMobileOneMinute(adminPhone))){
            throw new WoniuException("1分钟内不可再次发送",10001);
        }
        //检查一小时内是否发送过5次短信
        if(redisUtil.exist(RedisKey.sendMessageNumber(adminPhone))){
            int number=Integer.parseInt(redisUtil.getString(RedisKey.sendMessageNumber(adminPhone)));
            if(number>5){
                throw new WoniuException("超过发送上限,请稍后再试",10001);
            }
        }
        System.out.println(adminPhone);
        //将手机号码保存到消息队列中
        rabbitTemplate.convertAndSend("check_phone_queue",adminPhone);
    }

    @Override
    public void checkMessage(CheckMessageParam param) {
        //验证码验证
        if (!param.getCode().equals(redisUtil.getString(RedisKey.checkMobile(param.getMobile())))) {
            throw new WoniuException("验证码错误",10001);
        }
    }

    @Override
    public AdminSimpleDto adminCodeLogin(String adminPhone) {
        //根据管理员手机号判断账号
        QueryWrapper<AdminInfo> wrapper=new QueryWrapper<>();
        wrapper.eq("admin_phone",adminPhone);
        AdminInfo adminInfo = adminInfoService.getOne(wrapper);
        if(ObjectUtil.isEmpty(adminInfo)) throw new WoniuException("账号不存在",10001);
        //获取adminInfo转adminSimpleDto返回
        AdminSimpleDto dto = new AdminSimpleDto();
        dto.setAdminId(adminInfo.getAdminId());
        dto.setAdminPhone(adminPhone);
        dto.setAdminName(adminInfo.getAdminName());
        dto.setAdminRoleName(adminRoleService.getById(adminInfo.getAdminRoleId()).getAdminRoleName());
        return dto;
    }

    @Override
    public AdminSimpleDto adminLogin(AdminLoginParam param) {
        //根据管理员手机号判断账号
        QueryWrapper<AdminInfo> wrapper=new QueryWrapper<>();
        wrapper.eq("admin_phone",param.getAdminPhone());
        AdminInfo adminInfo = adminInfoService.getOne(wrapper);
        if(ObjectUtil.isEmpty(adminInfo)) throw new WoniuException("账号不存在",10001);
        //判断密码是否正确
        if(!adminInfo.getAdminPass().equals(param.getAdminPass())) throw new WoniuException("密码有误",10001);
        //获取adminInfo转adminSimpleDto返回
        AdminSimpleDto dto = new AdminSimpleDto();
        dto.setAdminId(adminInfo.getAdminId());
        dto.setAdminPhone(param.getAdminPhone());
        dto.setAdminName(adminInfo.getAdminName());
        dto.setAdminRoleName(adminRoleService.getById(adminInfo.getAdminRoleId()).getAdminRoleName());
        return dto;
    }

}
